package com.ljxy.score.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSON;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljxy.score.common.Result;
import com.ljxy.score.entity.UserDTO;
import com.ljxy.score.entity.domain.mystudent;
import com.ljxy.score.service.mystudentService;
import com.ljxy.score.mapper.mystudentMapper;
import com.ljxy.score.util.MD5Util;
import com.ljxy.score.util.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @description 针对表【mystudent】的数据库操作Service实现
 */
@Service
public class mystudentServiceImpl extends ServiceImpl<mystudentMapper, mystudent>
        implements mystudentService {


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result<?> register(String stuId, String password) {
        if (StringUtils.isAnyBlank(stuId, password)) {
            return Result.error("500", "参数错误");
        }
        //1.进行处理
        mystudent mystudent = new mystudent();
        mystudent.setStuId(stuId);
        //进行加密处理
        String passwordEncrypt = MD5Util.string2MD5(password);
        mystudent.setPassword(passwordEncrypt);
        boolean isSuccess = this.save(mystudent);
        if (!isSuccess) {
            return Result.error("500", "插入数据库失败");
        }
        return Result.success("200");
    }

    @Override
    public Result<?> login(String stuId, String password, Integer classId) {
        if (StringUtils.isAnyBlank(stuId, password)) {
            return Result.error();
        }
        QueryWrapper<mystudent> queryWrapper = new QueryWrapper<>();
        String passwordEncrypt = MD5Util.string2MD5(password);
        queryWrapper.eq("stu_id", stuId).eq("password", passwordEncrypt);
        int count = this.count(queryWrapper);
        if (count == 0) {
            return Result.error("500", "");
        }
        return Result.success(count);
    }

    @Override
    public R<?> importExcel(MultipartFile file) {
        String fileName = file.getName();
        EasyExcel.read(file.getName(), EasyExcel.class, new PageReadListener<EasyExcel>(dataList -> {
            for (EasyExcel demoData : dataList) {
                System.out.println(demoData);
            }
        })).sheet().doRead();
        return R.ok("");
    }
}




